package com.heeexy.example.controller.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heeexy.example.service.TokenService;
import com.heeexy.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import yinwei.bo.base.util.JwtUtils;
import yinwei.bo.common.annotation.Log;
import yinwei.bo.common.config.YaoMiConfig;
import yinwei.bo.common.entry.uList;
import yinwei.bo.common.entry.vo.stuLogVo;
import yinwei.bo.common.enums.BusinessType;
import yinwei.bo.common.exception.Assert;
import yinwei.bo.common.result.R;
import yinwei.bo.common.result.ResponseEnum;
import yinwei.bo.common.util.ServletUtils;
import yinwei.bo.common.util.StringUtils;
import yinwei.bo.common.util.file.FileUploadUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/user/profile")
public class UserInfoController
{
    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    /**
     * 修改用户
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R updateProfile(@RequestBody uList user)
    {
        String name= JwtUtils.getUserName(tokenService.getToken(ServletUtils.getRequest()));
        if (StringUtils.isNotEmpty(user.getPhone())
                && userService.checkMobile(user.getPhone()))
        {
            return R.error().message("修改用户'" + user.getName() + "'失败，手机号码已存在");
        }
        if (StringUtils.isNotEmpty(user.getEmail())
                && userService.checkEmail(user.getEmail()))
        {
            return R.error().message("修改用户'" + user.getName() + "'失败，邮箱账号已存在");
        }
        if (userService.updateUserProfile(user))
        {
            /*LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            // 更新缓存用户信息
            loginUser.getUser().setNickName(user.getNickName());
            loginUser.getUser().setPhonenumber(user.getPhonenumber());
            loginUser.getUser().setEmail(user.getEmail());
            loginUser.getUser().setSex(user.getSex());
            tokenService.setLoginUser(loginUser);*/
            return R.ok();
        }
        return R.error().message("修改个人信息异常，请联系管理员");
    }

    /**
     * 重置密码
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updatePwd")
    public R updatePwd(String oldPassword, String newPassword)
    {
        String name= JwtUtils.getUserName(tokenService.getToken(ServletUtils.getRequest()));
        String password=userService.findPassword(name);
        Assert.notEquals(oldPassword,password,ResponseEnum.OLD_PASSWORD);
        Assert.notEquals(newPassword, password, ResponseEnum.PASSWORD_EQUAL);
        uList uList=new uList();
        uList.setAccout(name);
        uList.setPassword(newPassword);
        if(userService.resetUserPwd(name,newPassword));
        {
            return R.ok();
        }
    }
    /**
     * 头像上传
     */
    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public R avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
    {

        if (!file.isEmpty())
        {
            String name=JwtUtils.getUserName(tokenService.getToken(ServletUtils.getRequest()));
            String avatar = FileUploadUtils.upload(YaoMiConfig.getAvatarPath(), file);
            if (userService.updateUserAvatar(name, avatar))
            {
                Map map=new HashMap();
                map.put("imgUrl", avatar);
                return R.ok().data(map);
            }
        }
        return R.error().message("上传图片异常，请联系管理员");
    }

    @RequestMapping("/findLog")
    public R findLog(@RequestParam(defaultValue = "1") Integer CurrentPage,
                     @RequestParam(defaultValue = "10") Integer PageSize){
        PageHelper.offsetPage((CurrentPage - 1) * PageSize,PageSize);
       long id=JwtUtils.getUserId(tokenService.getToken(ServletUtils.getRequest()));
        //System.out.println("id"+id);
        List<stuLogVo> stuLogVo=userService.findLog(id);
        PageInfo pageInfo = new PageInfo(stuLogVo);
        Map map=new HashMap();
        map.put("pageinfo",pageInfo);
        return R.ok().data(map);
    }
}
